package com.flex.showdoc4j.executor;

import com.flex.core.annotation.Doc;
import com.flex.core.annotation.DocParam;
import com.flex.showdoc4j.config.Configuration;
import com.flex.showdoc4j.executor.interfaces.DocResolver;
import com.flex.showdoc4j.factory.DocFactory;
import com.flex.showdoc4j.model.DocModel;
import com.flex.showdoc4j.model.ParamModel;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * @author ganlt<br>
 * @since 2019/06/26 15:35 <br>
 */
@Slf4j
public class SimpleDocResolver implements DocResolver {
    private Configuration configuration;

    public SimpleDocResolver(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public Wrapper lookMethod(String patternUrl, Method method) {
        Doc doc = method.getAnnotation(Doc.class);
        if (doc == null) {
            if (log.isInfoEnabled()) {
                log.info("请求:{}未实现接口定义", patternUrl);
            }
            return null;
        }

        DocModel docModel = DocFactory.createDocModel(doc);
        DocParam[] docParams = doc.params();
        List<ParamModel> paramModels = new ArrayList<>();
        Arrays.stream(Optional.of(docParams).orElse(new DocParam[]{})).forEach(p -> {
            ParamModel param = DocFactory.createParamModel(p);
            paramModels.add(param);
        });

        DocWrapper docWrapper = new DocWrapper();
        docWrapper.setDocModel(docModel);
        docWrapper.setParamModelList(paramModels);

        log.info("请求:{}接口封装对象:{},参数:{}", patternUrl, docModel, paramModels);
        return docWrapper;
    }
}
